package org.summer.lawyer.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.summer.common.core.exception.ServiceException;
import org.summer.common.core.utils.MapstructUtils;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.lawyer.domain.AccountApplication;
import org.summer.lawyer.domain.vo.accountapplication.AccountApplicationCreateVo;
import org.summer.lawyer.domain.vo.accountapplication.AccountApplicationVo;
import org.summer.lawyer.domain.vo.accountapplication.ApplyExportVo;
import org.summer.lawyer.mapper.AccountApplicationMapper;
import org.summer.lawyer.service.IAccountApplicationService;
import org.summer.lawyer.util.ParamsUtil;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class AccountApplicationServiceImpl extends ServiceImpl<AccountApplicationMapper, AccountApplication> implements IAccountApplicationService {


    @Override
    public void saveAccountApplication(AccountApplicationCreateVo vo) {
        AccountApplication accountApplication = new AccountApplication();
        MapstructUtils.convert(vo, accountApplication);
        accountApplication.setCreateTime(DateUtil.date());
        accountApplication.setReviewTime(DateUtil.date());
        accountApplication.setStatus("1");
        baseMapper.insert(accountApplication);
    }

    @Override
    public TableDataInfo<AccountApplicationVo> selectListPage(AccountApplicationVo vo, PageQuery pageQuery) {
        Map<String, Object> params = ParamsUtil.getParams(vo.getParams());
        LambdaQueryWrapper<AccountApplication> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StrUtil.isNotBlank(vo.getContactPhone()), AccountApplication::getContactPhone, vo.getContactPhone()).or()
                .like(StrUtil.isNotBlank(vo.getContactName()), AccountApplication::getContactName, vo.getContactName())
                .between(params.get("beginTime") != null && params.get("endTime") != null, AccountApplication::getCreateTime, params.get("beginTime"), params.get("endTime"))
                .orderByDesc(AccountApplication::getId);
        if (PageQuery.DEFAULT_PAGE_SIZE == pageQuery.getPageSize()) {
            return TableDataInfo.build(baseMapper.selectVoList(queryWrapper));
        }
        Page<AccountApplicationVo> page = baseMapper.selectVoPage(pageQuery.build(), queryWrapper);
        return TableDataInfo.build(page);
    }

    @DSTransactional(rollbackFor = Exception.class)
    @Override
    public void deleteAccountApplicationByIds(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            throw new ServiceException("主键不能为空");
        }
        ids.forEach(item -> {
            baseMapper.deleteById(item);
        });
    }

    @Override
    public AccountApplicationVo selectAccountApplicationVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    @Override
    public List<ApplyExportVo> selectApplyExportVoList(AccountApplicationVo vo) {
        Map<String, Object> params = vo.getParams();
        LambdaQueryWrapper<AccountApplication> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StrUtil.isNotBlank(vo.getContactPhone()), AccountApplication::getContactPhone, vo.getContactPhone()).or()
                .like(StrUtil.isNotBlank(vo.getContactName()), AccountApplication::getContactName, vo.getContactName())
                .between(params.get("beginTime") != null && params.get("endTime") != null, AccountApplication::getCreateTime, params.get("beginTime"), params.get("endTime"))
                .orderByDesc(AccountApplication::getId);
        return baseMapper.selectVoList(queryWrapper, ApplyExportVo.class);
    }
}
